package cn.xi.goods.service.impl;

import cn.xi.goods.dao.BrandMapper;
import cn.xi.goods.service.BrandService;
import cn.xi.pojo.Brand;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    //    查询所有
    @Override
    public List<Brand> findAll() {
        List<Brand> brands = brandMapper.selectAll();
        return brands;
    }

    //    根据id查询
    @Override
    public Brand findId(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    //    添加数据
    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    //    根据id修改数据
    @Override
    public void save(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    //    根据id删除数据
    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    //    条件查询
    @Override
    public Result<List<Brand>> getString(Map map) {
//        int i =1/0;
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        List<Brand> brands = null;
        if (map != null) {
            Boolean b = false;
            if (map.get("name") != null && "" != map.get("name")) {
                criteria.andLike("name", "%" + map.get("name") + "%");
                b = true;
            }
            if (map.get("letter") != null && "" != map.get("letter")) {
                criteria.andEqualTo("letter", map.get("letter"));
                b = true;
            }
            if (b) {
                brands = brandMapper.selectByExample(example);
            }
        } else {
            return new Result<>(false, StatusCode.ACCESSERROR, "查询失败", null);
        }

        return new Result<>(true, StatusCode.OK, "查询成功", brands);
    }

    @Override
    public PageResult<Brand> pageFind(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Page<Brand> brands = (Page<Brand>) brandMapper.selectAll();

        return new PageResult<>(brands.getTotal(), brands.getResult());
    }

    @Override
    public Result<List<Brand>> getPageString(Map map, Integer page, Integer size) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        PageResult pageResult = null;
        if (map != null) {
            Boolean b = false;
            if (map.get("name") != null && "" != map.get("name")) {
                criteria.andLike("name", "%" + map.get("name") + "%");
                b = true;
            }
            if (map.get("letter") != null && "" != map.get("letter")) {
                criteria.andEqualTo("letter", map.get("letter"));
                b = true;
            }
            if (b) {
                PageHelper.startPage(page, size);
                Page<Brand> brands = (Page<Brand>) brandMapper.selectByExample(example);
                pageResult = new PageResult(brands.getTotal(), brands.getResult());
            }
        } else {
            return new Result<>(false, StatusCode.OK, "查询失败", pageResult);
        }
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);


    }

    @Override
    public List<Map> findCategoryByBrand(String name) {
        return  brandMapper.findCategoryByBrand(name);

    }


}
